
insert into asm_aegg_estrai_dati_cla
 
select acons.pod,

CASE WHEN cons_gg*ggcontr< 5001 and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'  THEN                'Fino a       5MWh' else
          case when cons_gg*ggcontr < 10001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN     'Fino a      10MWh' else
          case when cons_gg*ggcontr < 15001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN     'Fino a      15MWh' else
          case when cons_gg*ggcontr < 20001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN     'Fino a      20MWh' else
          case when cons_gg*ggcontr < 50001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN     'Fino a      50MWh' else
          case when cons_gg*ggcontr < 100001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN    'Fino a     100MWh' else
          case when cons_gg*ggcontr < 500001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN    'Fino a     500MWh' else
          case when cons_gg*ggcontr < 2000001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN   'Fino a   2.000MWh' else
          case when cons_gg*ggcontr < 20000001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN  'Fino a  20.000MWh' else
          case when cons_gg*ggcontr < 50000001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN  'Fino a  50.000MWh' else
          case when cons_gg*ggcontr < 70000001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN  'Fino a  70.000MWh' else
          case when cons_gg*ggcontr < 150000001  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN 'Fino a 150.000MWh' else
       case when cons_gg*ggcontr < 999999999999  and GG_ANNO.USO <> 'R' AND GG_ANNO.USO <> 'N'THEN 'Oltre  150.000MWh' else 
          
          
    case when cons_gg*ggcontr < 1001 and gg_anno.uso in('R','N') then                               'Fino a  1.000KWh' else
            case when cons_gg*ggcontr < 1801  and gg_anno.uso in('R','N')     then                  'Fino a  1.800KWh' else
                 case when cons_gg*ggcontr < 2501  and gg_anno.uso in('R','N') then                 'Fino a  2.500KWh' else
                   case when cons_gg*ggcontr < 3501  and gg_anno.uso in('R','N') then               'Fino a  3.500KWh' else
                      case when cons_gg*ggcontr < 5001  and gg_anno.uso in('R','N')  then           'Fino a  5.000KWh' else
                           case when cons_gg*ggcontr < 15001  and gg_anno.uso in('R','N')  then     'Fino a 15.000KWh' else 
                                                                                                    'Oltre  15.000KWh'   
           
                                
          end end end end end end end end end end end end end end end end end end end classe,
          decode(GG_ANNO.USO,'R','R','N','N','M','M','A') USO,
          potenza               
from

  (select pod,max(data_fine_periodo)-min(data_inizio_periodo) gg_cons,
  sum(tot_qta),
  sum(tot_qta)/(max(data_fine_periodo)-min(data_inizio_periodo)+0.00000000000000001) cons_gg 
  from asm_aegg_estrai_dati_qta 
  where 
  progressivo_voce in ('010','010TRAS')
  group by pod) acons,
 ( select pod,MAX(VV.CODICE_TIPO_USO) USO,MAX(VV.POTENZA_IMPEGNATA) POTENZA,
case when to_number(max(vv.data_chiusura_richiesta)-min(vv.dar)+1) > 365 
        then 365 else to_number(max(vv.data_chiusura_richiesta)-min(vv.dar)+1) end  ggcontr
   from asm_aegg_estrai_dati vv
  where
  vv.progressivo_voce in ('010','010TRAS')
    group by pod) gg_anno
    where
    gg_anno.pod=acons.pod